package com.aphrodite.io.sdp;

import java.util.Vector;

import com.aphrodite.util.StringParser;

/**
 * SDP media field.
 * <p>
 * <BLOCKQUOTE>
 * 
 * <PRE>
 *      media-field = &quot;m=&quot; media SP port [&quot;/&quot; integer] SP proto 1*(SP fmt) CRLF
 * </PRE>
 * 
 * </BLOCKQUOTE>
 */
public final class MediaField extends SdpField {
	/** Creates a new SdpField. */
	public MediaField(SdpField sf) {
		super(sf);
	}

	/** Creates a new MediaField. */
	public MediaField(String media_field) {
		super('m', media_field);
	}

	/** Creates a new MediaField. */
	public MediaField(String media, int port, int num, String transport, String formats) {
		super('m', null);
		value = media + " " + port;
		if (num > 0)
			value += "/" + num;
		value += " " + transport + " " + formats;
	}

	/**
	 * Creates a new MediaField.
	 * 
	 * @param formatlist
	 *            a Vector of media formats (properly a Vector of Strings)
	 */
	public MediaField(String media, int port, int num, String transport, Vector formatlist) {
		super('m', null);
		value = media + " " + port;
		if (num > 0)
			value += "/" + num;
		value += " " + transport;
		for (int i = 0; i < formatlist.size(); i++)
			value += " " + formatlist.elementAt(i);
	}

	/** Gets the media formats as a Vector of String. */
	public Vector getFormatList() {
		Vector formatlist = new Vector();
		StringParser par = new StringParser(value);
		par.skipString().skipString().skipString();
		while (par.hasMore()) {
			String fmt = par.getString();
			if (fmt != null && fmt.length() > 0)
				formatlist.addElement(fmt);
		}
		return formatlist;
	}

	/** Gets the media formats. */
	public String getFormats() {
		return (new StringParser(value)).skipString().skipString().skipString().skipWSP().getRemainingString();
	}

	/** Gets the media type. */
	public String getMedia() {
		return new StringParser(value).getString();
	}

	/** Gets the media port. */
	public int getPort() {
		String port = (new StringParser(value)).skipString().getString();
		int i = port.indexOf('/');
		if (i < 0)
			return Integer.parseInt(port);
		else
			return Integer.parseInt(port.substring(0, i));
	}

	/** Gets the transport protocol. */
	public String getTransport() {
		return (new StringParser(value)).skipString().skipString().getString();
	}

}
